package com.jxl.coreplay.flex
{
	import com.jxl.coreplay.controls.IVideo;
	import com.jxl.coreplay.events.VideoEvent;
	
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.geom.Rectangle;
	import flash.media.Video;
	import flash.net.NetStream;
	
	import mx.core.UIComponent;
	
	public class UIVideo extends UIComponent implements IVideo
	{
		
		protected var _video:Video;
		protected var _maintainAspectRatio:Boolean 		= true;
		
		protected var videoDebugOutline:Shape;
		protected var _showDebugVideoOutline:Boolean 	= false;
		
		public function get video():Video { return _video; }
		
		public function get showDebugVideoOutline():Boolean { return _showDebugVideoOutline; }
		public function set showDebugVideoOutline(value:Boolean):void
		{
			_showDebugVideoOutline = value;
			invalidateDisplayList();
		}
		
		public function get maintainAspectRatio():Boolean { return _maintainAspectRatio; }
		public function set maintainAspectRatio(value:Boolean):void
		{
			_maintainAspectRatio = value;
			invalidateDisplayList();
		}
		
		public function UIVideo():void
		{
			super();
		}
		
		protected override function createChildren():void
		{
			super.createChildren();
			
			if(_video == null)
			{
				_video 				= new Video();
				_video.smoothing 	= true;
				addChild(_video);
			}
		}
		
		protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			if(_video.videoWidth == 0 || _video.videoHeight == 0)
			{
				_video.width        = width;
				_video.height       = height;
				return;
			}
			
			if(_maintainAspectRatio == true)
			{
				var aspectRatio:Number;
				if(_video.videoWidth >= _video.videoHeight)
				{
					aspectRatio					= _video.videoWidth / _video.videoHeight;
					var desiredHeight:Number 	= width / aspectRatio;
					
					if(desiredHeight <= height)
					{
						_video.width 			= width;
						_video.height 			= desiredHeight;
					}
					else
					{
						_video.width 			= aspectRatio * height;
						_video.height 			= height;
					}
				}
				else
				{
					aspectRatio 				= _video.videoHeight / _video.videoWidth;
					var desiredWidth:Number 	= height / aspectRatio;
					
					if(desiredWidth <= width)
					{
						_video.width 			= desiredWidth;
						_video.height 			= height;
					}
					else
					{
						_video.width 			= width;
						_video.height 			= aspectRatio * width;
					}
				}
			}
			else
			{
				_video.width 					= width;
				_video.height 					= height;
			}
			
			drawDebugVideoOutline();
		}
		
		protected function drawDebugVideoOutline():void
		{
			if(_showDebugVideoOutline == true)
			{
				if(videoDebugOutline == null)
				{
					videoDebugOutline = new Shape();
					addChild(videoDebugOutline);
					setChildIndex(videoDebugOutline, numChildren - 1);
					
				}
				var g:Graphics = videoDebugOutline.graphics;
				g.clear();
				g.lineStyle(0, 0xFF00FF);
				g.drawRect(_video.x, _video.y, _video.width, _video.height);
				g.endFill();
			}
			else
			{
				if(videoDebugOutline != null)
				{
					videoDebugOutline.graphics.clear();
					if(contains(videoDebugOutline) == true) removeChild(videoDebugOutline);
					videoDebugOutline = null;
				}
			}
		}
		
		public function attachNetStream(netStream:NetStream):void
		{
			_video.attachNetStream(netStream);
		}
		
		public function get videoRectangle():Rectangle
		{
			return new Rectangle(_video.x, _video.y, _video.videoWidth, _video.videoHeight);
		}
		
		public function videoSizeChanged(oldRectangle:Rectangle, newRectangle:Rectangle):void
		{
			var evt:VideoEvent = new VideoEvent(VideoEvent.VIDEO_SIZE_CHANGED);
			evt.oldVideoSize = oldRectangle;
			evt.newVideoSize = newRectangle;
			dispatchEvent(evt);
		}
	}
}